/**
 ******************************************************************************
 * @file main.c
 * @author GX (2524913364@qq.com)
 * @brief 
 * @version 1.0.0 
 * @date 2022-07-04
 * 
 * @copyright Copyright (c) 2022
 * 
 *----------------------------------------------------------------------------*
 *  Remark         : Description                                              *
 *----------------------------------------------------------------------------*
 *  Change History :                                                          *
 *  <Date>     | <Version> | <Author>       | <Description>                   *
 *----------------------------------------------------------------------------*
 *  2022-07-04 | 1.0.0     | GX             | Create file                     *
 *----------------------------------------------------------------------------*
 *                                                                            *
 ******************************************************************************
 */
/*----------------------------- start of file -------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <pthread.h>

/* 目录文件的头文件 */
#include <sys/types.h> 
#include <dirent.h>

#include <sys/types.h> 
#include <sys/stat.h> 
#include <sys/ioctl.h>


#include <linux/input.h>

#include "debug.h"
#include "lcd.h"
#include "touch_screen.h"
#include "file_operation.h"
#include "app_boot.h"
#include "app_unlock.h"
#include "app_desktop.h"
#include "app_photo.h"
#include "app_artmaster.h"
#include "app_pokemon.h"

int main(int argc, char* argv[])
{
#if 1
  /*------------------------------开启显示屏幕-----------------------------*/
	// int fd_lcd;                             /* 打开文件获取文件描述 */
  // int *fb_base = NULL;                    /* 映射后显存的地址 */
	// /* 打开文件 */
	// fd_lcd = open(LCD_PATH, O_RDWR);
	// if(fd_lcd == -1)
	// {
	// 	perror("open lcd");
	// }
  // else
	// {
	// 	/* 显存映射 */
	// 	fb_base = mmap(NULL, LCD_BUFF_SIZE*LCD_BLOCK,  PROT_READ|PROT_WRITE,
	// 				MAP_SHARED, fd_lcd, 0);
	// 	if(fb_base == MAP_FAILED) 
	// 	{
	// 		perror("申请显存失败");
	// 	} 
	// 	else
	// 	{
	// 		LCD_CLEAN(fb_base);
	// 	}
	// }
  
  /*--------------------------------开机动画--------------------------------*/
#if 1
  if( play_boot_animation() )
  {
    printf("boot animation play over\n");
  }
  else
  {
    printf("boot animation play error\n");
  }
#endif 


  /* 初始化触摸屏开启状态机-------------------------- */
  pthread_t id = InitTouch_pthread();
  INTERFACE_CHOOSE interface = INTERFACE_UNLOCK;
  while(1)
  {
    switch (interface)
    {
    /*--------------------------------解锁界面--------------------------------*/
    case INTERFACE_UNLOCK:
      if( unlock_interface() )
      {
        interface = INTERFACE_DESKTOP;
        printf("unlock interface over\n");
      }
      else
      {
        printf("unlock interface error\n");
      }
      break;
    /*---------------------------------桌面---------------------------------*/
    case INTERFACE_DESKTOP:
      printf("ready menu--------\n");
      interface = desktop_interface();
      
      break;

    /*---------------------------------相册---------------------------------*/
    case INTERFACE_PHOTO:
      if( app_photo() )
      {
        interface = INTERFACE_DESKTOP;
        printf("photo over\n");
      }
      else
      {
        printf("photo error\n");
      }
      break;

    /*---------------------------------画板---------------------------------*/
    case INTERFACE_DRAW:
      if( app_DrawingBoard() )
      {
        interface = INTERFACE_DESKTOP;
        printf("artmaster over\n");
      }
      else
      {
        printf("artmaster error\n");
      }
      break;
    /*--------------------------------宝可梦--------------------------------*/
    case INTERFACE_POKEMON:
      if( app_Pokemon() )
      {
        interface = INTERFACE_DESKTOP;
        printf("pokemon over\n");
      }
      else
      {
        printf("pokemon error\n");
      }
      break;
    default:
      break;
    }
  }
  


  /* 取消线程 */


  printf("end--------\n");


#endif


#if 0
  /*------------------------------状态机测试-----------------------------*/
	int fd_lcd;                             /* 打开文件获取文件描述 */
  int *fb_base = NULL;                    /* 映射后显存的地址 */
	/* 打开文件 */
	fd_lcd = open(LCD_PATH, O_RDWR);
	if(fd_lcd == -1)
	{
		perror("open lcd");
	}
  else
	{
		/* 显存映射 */
		fb_base = mmap(NULL, LCD_BUFF_SIZE*LCD_BLOCK,  PROT_READ|PROT_WRITE,
					MAP_SHARED, fd_lcd, 0);
		if(fb_base == MAP_FAILED) 
		{
			perror("申请显存失败");
		} 
		else
		{
			LCD_CLEAN(fb_base);
		}
	}
  DispStringLine_EN(fb_base, 1, 0, "STATE_NOT_PRESSED");


	/*-------------------------------触摸屏----------------------------------*/
  TOUCH tc;
	InitTouch_pthread();	/* 开启触摸屏线程 */

  while(1)
  {
		get_tcState(&tc);
    // if(tc.state == STATE_NOT_PRESSED)
    // printf("1");
    // if( tc.response == true )
    // {
    //   continue;
    // }

    LCD_ClearLine(fb_base, 1);   

    switch (tc.state)
		{
			case STATE_NOT_PRESSED:	/* 无操作 */
        DispStringLine_EN(fb_base, 1, 0, "STATE_NOT_PRESSED");
        response_over();
				break;

			case STATE_PRESSED:			/* 按下 */
				DispStringLine_EN(fb_base, 1, 0, "STATE_PRESSED");
        response_over();
				break;

			case STATE_RELEASE:			/* 释放 */
				DispStringLine_EN(fb_base, 1, 0, "STATE_RELEASE");
        response_over();
				break;

      case STATE_SIN_OR_DOU:/* 判断单击还是双击 */
				DispStringLine_EN(fb_base, 1, 0, "STATE_SIN_OR_DOU");
        response_over();
				break;

			case STATE_SINGLE_CLICK:/* 单击 */
				DispStringLine_EN(fb_base, 1, 0, "STATE_SINGLE_CLICK");
        response_over();
				break;

			case STATE_DOUBLE_CLICK:/* 双击 */
				DispStringLine_EN(fb_base, 1, 0, "STATE_DOUBLE_CLICK");
        response_over();
				break;

			case STATE_SLIDING:				/* 滑屏 */
        DispStringLine_EN(fb_base, 1, 0, "STATE_SLIDING");
        response_over();
				break;
			
      case STATE_DTAGING:				/* 拖动 */
        DispStringLine_EN(fb_base, 1, 0, "STATE_DTAGING");
        response_over();
				break;

			default:
				break;
		}
  }
#endif


	return 0;

}

  




 

/*------------------------------ end of file --------------------------------*/
